package com.lightdz.tshirtdz.bll;

import com.lightdz.tshirtdz.dal.impl.FavoriteDal;
import com.lightdz.tshirtdz.dal.impl.ProductDal;
import com.lightdz.tshirtdz.model.api.ApiPStyle;
import com.lightdz.tshirtdz.model.api.ApiResponse;
import com.lightdz.tshirtdz.model.api.ApiShoppingCar;
import com.lightdz.tshirtdz.model.entity.FavoriteEntity;
import com.lightdz.tshirtdz.model.entity.ProductEntity;

import com.lightdz.tshirtdz.utils.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ Description：${description}
 * @ Author     ：zbl
 * @ Date       ：Created in 16:54 09/08/2018
 * @ Modified By：
 */
@Repository
public class FavoriteBll {

    @Autowired
    FavoriteDal favoriteDal;
    @Autowired
    ProductDal productDal;


    /**
     * @param wxId
     * @param pid
     * @param isFav
     * @return
     */
    public ApiResponse<Boolean> setFavorite(String wxId, long pid, boolean isFav) {
        ApiResponse<Boolean> apiResponse = ApiResponse.getNoResponse();
        try {
            boolean result = false;
            if (isFav)
                result = favoriteDal.addFavorite(wxId, pid);
            else
                result = favoriteDal.deleteFavorite(wxId, pid);
            apiResponse = new ApiResponse<>(result);
        } catch (Exception ex) {
            ex.printStackTrace();
            LogHelper.error("FavoriteBll.setFavorite", ex);
        }
        return apiResponse;
    }

    public ApiResponse<List<ApiShoppingCar>> getFavList(String wxId) {
        ApiResponse<List<ApiShoppingCar>> apiResponse = ApiResponse.getNoResponse();

        List<ApiShoppingCar> result = null;
        try {
            result = new ArrayList<>();

            //拿到所有的收藏列表
            List<FavoriteEntity> favList = favoriteDal.getFavoriteList(wxId);
            if (!favList.isEmpty()) {

                ApiShoppingCar shoppingCar = new ApiShoppingCar();

                for (FavoriteEntity p : favList) {
                    ProductEntity pEntity = productDal.getProduct(p.getPid());
                    if (pEntity != null) {
                        shoppingCar.setDesc(pEntity.getpDesc());
                        shoppingCar.setBannerPicUrl(pEntity.getpBannerPicUrl());
                        shoppingCar.setPrice((pEntity.getpPrice()));

                        ApiPStyle pStyle = new ApiPStyle();
                        pStyle.setPid(p.getPid());
                        pStyle.setWxId(wxId);
                        pStyle.setColorList(Arrays.asList(pEntity.getpColors().split(",")));
                        pStyle.setSizeList(Arrays.asList(pEntity.getpSizes().split(",")));
                        pStyle.setpCount(0);
                        shoppingCar.setpStyle(pStyle);

                        result.add(shoppingCar);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return apiResponse;
    }
}
